package com.suneast.suneaststore.modules.goodsdetail.subfragment;

import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.suneast.suneaststore.R;
import com.suneast.suneaststore.modules.goodsdetail.GoodsDetailFragment;
import com.suneast.suneaststore.modules.goodsdetail.GoodsHandler;
import com.suneast.suneaststore.modules.goodsdetail.adapter.ProductAdapter;
import com.suneast.suneaststore.modules.goodsdetail.event.BottomPageChangeEvent;
import com.suneast.suneaststore.modules.goodsdetail.event.SubCateLoaded;
import com.suneast.suneaststore.bean.homepage.RecommendProductBean;
import com.suneast.suneaststore.ui.fragments.BaseFragment;
import com.suneast.suneaststore.ui.loader.LoaderStyle;
import com.suneast.suneaststore.util.log.LatteLogger;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class GoodsRecommendFragment extends BaseFragment {
    @Override
    public @LayoutRes
    int setLayout() {
        return R.layout.fragment_goods_recommend;
    }

    private int merchantId = -1;

    public GoodsRecommendFragment withMerchId(int merchantId) {
        this.merchantId = merchantId;
        return this;
    }

    @BindView(R.id.rv_recommend)
    RecyclerView recyclerView;
    ProductAdapter adapter;

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        EventBus.getDefault().register(this);
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2, GridLayoutManager.VERTICAL, false));
        adapter = new ProductAdapter(R.layout.item_star_product);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener((BaseQuickAdapter adapter, View view, int position) -> {
            RecommendProductBean bean = (RecommendProductBean) adapter.getData().get(position);
            GoodsDetailFragment fragment = new GoodsDetailFragment().withId(bean.getId());
            if (merchantId != -1) {
                fragment.withMerchId(merchantId);
            }
            getParentDelegate().start(fragment);
        });
        Bundle bundle = getArguments();
        if (bundle != null) {
            int id = bundle.getInt("id");
            GoodsHandler.requestGoodsRecommand(id);
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onPageChange(BottomPageChangeEvent event) {
        if (event.getIndex() == 3) {
            if (event.getResponse() != null) {
                LatteLogger.json("test", event.getResponse());
                JSONObject rootJObj = JSONObject.parseObject(event.getResponse());
                if (rootJObj.getIntValue("code") == 1) {
                    JSONArray jsonArray = rootJObj.getJSONArray("data");
                    if (jsonArray != null && jsonArray.size() > 0) {
                        List<RecommendProductBean> dataList =
                                JSON.parseArray(jsonArray.toJSONString(),
                                        RecommendProductBean.class);
                        adapter.setNewData(dataList);
                        EventBus.getDefault().post(new SubCateLoaded());
                    }
                }
            }
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);

    }
}
